package com.koron.css2.common.utils;

import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class TxtUtils {
	
	private static Logger log = LogManager.getLogger(TxtUtils.class);
	
	private static final String LINE = System.getProperty("line.separator");
	
	private static final String DEFAULT_SEPARATOR = "\\|";
	
	public static String[] readTxtHeader(byte[] buf){
		try {
			String data = IOUtils.toString(buf, "UTF-8");
			String[] rows = data.split(LINE);
			return rows[0].split(DEFAULT_SEPARATOR);
		} catch (Exception e) {
			log.error("读取TXT文件失败 原因：" + e.getMessage());
			throw new RuntimeException(e);
		}
	}
	
	public static String[] readTxtHeader(byte[] buf, String separator){
		try {
			String data = IOUtils.toString(buf, "UTF-8");
			String[] rows = data.split(LINE);
			return rows[0].split(separator);
		} catch (Exception e) {
			log.error("读取TXT文件失败 原因：" + e.getMessage());
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 导入TXT 默认的分隔符为逗号 ,
	 * @param data
	 * @param title
	 * @param filename
	 * @return
	 */
	public static List<String[]> readTxtData(byte[] buf){
		List<String[]> result = new ArrayList<String[]>();
		try {
			String data = IOUtils.toString(buf, "GBK");
			String[] rows = data.split(LINE);
			for(String row : rows) {
				String[] cols = row.split(DEFAULT_SEPARATOR);
				result.add(cols);
			}
		} catch (Exception e) {
			log.error("读取TXT文件失败 原因：" + e.getMessage());
			throw new RuntimeException(e);
		}
		return result;
	}
	
	public static List<String[]> readTxtData(byte[] buf, String separator){
		List<String[]> result = new ArrayList<String[]>();
		try {
			String data = IOUtils.toString(buf, "UTF-8");
			String[] rows = data.split(LINE);
			for(String row : rows) {
				String[] cols = row.split(separator);
				result.add(cols);
			}
		} catch (Exception e) {
			log.error("读取TXT文件失败 原因：" + e.getMessage());
			throw new RuntimeException(e);
		}
		return result;
	}
	
	/**
	 * 导出TXT文件
	 * @param data 需要导出TXT的文件内容
	 * @param title 需要导出的TXT文件的标题
	 * @param filename 需要导出的TXT文件的名称
	 * @return
	 */
	public static HttpEntity<?> exportTxt(List<?> data, String title, String filename){
		try {
			Objects.requireNonNull(title, "导出的标题不能为空");
			Objects.requireNonNull(filename, "文件名不能为空");
			String line = System.getProperty("line.separator");
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			out.write(title.getBytes());
			out.write(line.getBytes());
			for(Object str : data) {
				out.write(Objects.toString(str, "null").getBytes("UTF-8"));
				out.write(line.getBytes());
			}
			HttpHeaders header = new HttpHeaders();
			String fn = URLEncoder.encode(filename, "UTF-8"); // new String(fileName.getBytes(),"ISO8859-1");
			header.add("Content-Disposition", "attachment ; filename=" + fn + ".txt");
			header.add("Content-Type", "text/plain");
			return new ResponseEntity<byte[]>(out.toByteArray(), header, HttpStatus.OK);
		}catch(Exception ex) {
			log.error("导出 txt 文件失败" + ex);
			return new ResponseEntity<String>("导出 txt 文件失败", HttpStatus.OK);
		}
	}

}
